//
//  JMPersonViewController.m
//  JMDome_1
//
//  Created by ios on 15/7/1.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMPersonViewController.h"
#import "JMCoustmButton.h"
#import "JMPersonMessViewController.h"
#import "JMSetViewController.h"
#import "JMZujiViewController.h"
#import "JMLoginViewController.h"


@interface JMPersonViewController ()

@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) UIButton *topImageView;

@property (nonatomic, strong) UIButton *setBtn;

@property (nonatomic, strong) JMCoustmButton *personMessbtn;

@property (nonatomic, strong) JMCoustmButton *zujiBtn;

@property (nonatomic, strong) UILabel *typeLabel;

@end


@implementation JMPersonViewController


- (UIImageView *)backImageView
{
    if (!_backImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"personMessage.bundle/me_backImage"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.userInteractionEnabled = YES;
        imageView.backgroundColor = [UIColor clearColor];
        _backImageView = imageView;
        [self.view addSubview:_backImageView];
    }
    return _backImageView;
}
- (UIButton *)topImageView
{
    if (!_topImageView) {
        UIButton *imageView = [[UIButton alloc]init];
        [imageView setImage:[UIImage imageNamed:@"personMessage.bundle/me_image"] forState:UIControlStateNormal];
        [imageView addTarget:self action:@selector(topIconButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backImageView addSubview:_topImageView = imageView];
    }
    return _topImageView;
}

- (UIButton *)setBtn
{
    if (!_setBtn) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"personMessage.bundle/set_image"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(setButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backImageView addSubview:_setBtn = button];
    }
    return _setBtn;
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:15.0f];
        label.textAlignment =NSTextAlignmentCenter;
        _typeLabel = label;
    }
    return _typeLabel;
}


- (JMCoustmButton *)personMessbtn
{
    if (!_personMessbtn) {
        _personMessbtn = [[JMCoustmButton alloc]initWithFrame:CGRectZero WithNormalImage:@"personMessage.bundle/xinxi_image" SelectedImage:@""];
        [_personMessbtn addTarget:self action:@selector(personMessageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backImageView addSubview:_personMessbtn];
    }
    return _personMessbtn;
}

- (JMCoustmButton *)zujiBtn
{
    if (!_zujiBtn) {
        _zujiBtn = [[JMCoustmButton alloc]initWithFrame:CGRectZero WithNormalImage:@"personMessage.bundle/zuji_image" SelectedImage:@""];
        [_zujiBtn addTarget:self action:@selector(zujiButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.backImageView addSubview:_zujiBtn];
    }
    return _zujiBtn;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
    UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(self.topImageView.frameLeft + 15, self.topImageView.frameBottom + 5, self.topImageView.frameWidth - 30, 30)];
    imageView.image  =[UIImage imageNamed:@"ship.bundle/botton"];
    imageView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:imageView];
    self.typeLabel.frame = CGRectMake(0, 0, imageView.frameWidth, imageView.frameHeight);
    [imageView addSubview:self.typeLabel];

    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if([cf.user.state intValue] == 1){
        self.typeLabel.text = @"已登陆";
    }else{
        self.typeLabel.text = @"未登陆";
    }
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.backImageView.frame = self.view.bounds;
    self.topImageView.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.3828/2.0, UI_SCREEN_HEIGHT *0.15845, UI_SCREEN_WITH *0.3828, UI_SCREEN_WITH *0.3828);
    
    
    self.setBtn.frame = CGRectMake(UI_SCREEN_WITH *0.6875, UI_SCREEN_HEIGHT *0.1276,UI_SCREEN_WITH *0.11718, UI_SCREEN_WITH *0.11718);
    self.personMessbtn.frame = CGRectMake(UI_SCREEN_WITH *0.109375, self.topImageView.frameBottom + UI_SCREEN_HEIGHT *0.11443, UI_SCREEN_WITH *0.3375, UI_SCREEN_WITH *0.353125);
    self.zujiBtn.frame = CGRectMake(UI_SCREEN_WITH *0.109375 + (UI_SCREEN_WITH *0.3375 + UI_SCREEN_WITH *0.109375), self.topImageView.frameBottom + UI_SCREEN_HEIGHT *0.11443, UI_SCREEN_WITH *0.3375, UI_SCREEN_WITH *0.353125);
}
#pragma mark  设置方法点击
-(void)setButtonClick:(id)sender{
    
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if([cf.user.state integerValue] == 1){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [JMAppNetworkClient token_WithInfo:dict complete:^(id data, NSError *error) {        if([data[@"status"] integerValue] == 1){
            JMSetViewController *vc =[JMSetViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [JMTools showAlertView:self.view message:data[@"message"]];
            [self goToRegistViewController];
        }
        }];
    }else{
        [self goToRegistViewController];
        
    }
}
-(void)personMessageButtonClick:(id)sender{
    
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if([cf.user.state integerValue] == 1){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [JMAppNetworkClient token_WithInfo:dict complete:^(id data, NSError *error) {        if([data[@"status"] integerValue] == 1){
            JMPersonMessViewController *vc = [JMPersonMessViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [JMTools showAlertView:self.view message:data[@"message"]];
            [self goToRegistViewController];
        }
        }];
    }else{
        [self goToRegistViewController];
        
    }
}
-(void)zujiButtonClick:(id)sender{
    
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if([cf.user.state integerValue] == 1){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [JMAppNetworkClient token_WithInfo:dict complete:^(id data, NSError *error) {        if([data[@"status"] integerValue] == 1){
            JMZujiViewController *vc = [JMZujiViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [JMTools showAlertView:self.view message:data[@"message"]];
             [self goToRegistViewController];
        }
        }];
    }else{
        [self goToRegistViewController];
        
    }
}

- (void)goToRegistViewController{
    JMLoginViewController *vc = [JMLoginViewController new];
    JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}
#pragma mark  重写放回方法
-(void)backBtnClick:(id)sender{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

-(void)topIconButtonClick:(UIButton *)sender{
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if([cf.user.state integerValue] == 1){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [JMAppNetworkClient token_WithInfo:dict complete:^(id data, NSError *error) {        if([data[@"status"] integerValue] == 1){
            JMPersonMessViewController *vc = [JMPersonMessViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [JMTools showAlertView:self.view message:data[@"message"]];
            [self goToRegistViewController];
        }
        }];
    }else{
        [self goToRegistViewController];

    }
}

@end

